using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace JH
{
    /// <summary>
    ///     toggle组件打开时打开物体，关闭时关闭物体
    /// </summary>
    [ExecuteAlways]
    public class ToggleSetActive : BaseToggleCustomEvent
    {
        /// <summary>
        ///     需要被设置 active 的物体
        /// </summary>
        [SerializeField] private List<GameObject> gameObjects = new();

        protected override void OnToggleOff()
        {
            base.OnToggleOff();
            SetActive(false);
        }

        protected override void OnToggleOn()
        {
            base.OnToggleOn();
            SetActive(true);
        }

        /// <summary>
        ///   设置物体的 active 状态
        /// </summary>
        private void SetActive(bool active)
        {
            foreach (var each in gameObjects.Where(i => i != null))
                each.SetActive(active);
        }
    }
}